package com.bazhua.bazo.controller;


import com.bazhua.bazo.common.utils.JwtUtil;
import com.bazhua.bazo.common.wxapplet.model.WxAccountResponse;
import com.bazhua.bazo.common.wxapplet.model.WxLoginRequest;
import com.bazhua.bazo.repository.model.WxUserSearch;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.ICommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api(tags = "微信用户接口")
@RestController
@RequestMapping("/api/wxuser")
public class WxUserController {

    @Autowired
    private ICommonService commonService;


    @RequestMapping(value = "/GetWinXinOpenId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult GetWinXinOpenId(@RequestParam(required = true, value = "code", defaultValue = "") String code) throws IOException {
        return commonService.GetWeiXinOpenId(code);
    }

    @ApiOperation(value = "authorization", notes = "用户授权")
    @RequestMapping(value = "/user/authorization", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult wxUserAuthorization(String code) {
        if (code == null || code.equals("")) {
            return ResultUtils.error(400, "缺少参数code或code不合法");
        } else {
            return commonService.wxUserAuthorization(code);
        }
    }

    @ApiOperation(value = "login", notes = "用户登录")
    @RequestMapping(value = "/user/login", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult wxAppletLoginApi(@RequestBody WxLoginRequest request) {
        return commonService.getWxUserInfo(request);
    }

    @ApiOperation(value = "login", notes = "用户登录(new)")
    @RequestMapping(value = "/user/new/login", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult wxAppletLoginApi(@RequestBody WxAccountResponse wxAccountResponse) {
        return commonService.getWxUserInfoNew(wxAccountResponse);
    }

    @ApiOperation(value = "getPhone", notes = "获取用户手机号")
    @RequestMapping(value = "/user/getPhone", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult updateWxUserPhone(@RequestBody WxLoginRequest request) {
        return commonService.updateWxUserPhone(request);
    }

    @ApiOperation(value = "registerCheckUser", notes = "注册核销助手")
    @RequestMapping(value = "/user/registerCheckUser", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult registerCheckUser(@RequestHeader String token, Long shopId) {
        String openId = JwtUtil.getWxOpenIdByToken(token);
        return commonService.registerCheckUser(openId, shopId);
    }
}
